define(["jquery", "bootstrap", "backend", "table", "form"], function (
  $,
  undefined,
  Backend,
  Table,
  Form
) {
  var Controller = {
    index: function () {
      // 初始化表格参数配置
      Table.api.init({
        extend: {
          index_url: "category/index",
          add_url: "category/add",
          edit_url: "category/edit",
          del_url: "category/del",
          multi_url: "category/multi",
          dragsort_url: "ajax/weigh",
          table: "category",
        },
      });

      var table = $("#table");
      var tableOptions = {
        url: $.fn.bootstrapTable.defaults.extend.index_url,
        pk: "id",
        sortName: "sort",
        pagination: true,
        commonSearch: true,
        search: false,
        searchFormVisible: true,
         showToggle: false,
        showColumns: false,
        showExport: false,
        columns: [
          [
            { checkbox: true },
            {
              field: "name",
              title: "分类名称",
              operate: "LIKE",
              align: "left",
              formatter: function (value, row, index) {
                return value.toString().replace(/(&|&amp;)nbsp;/g, "&nbsp;");
              },
            },
            {
              field: "type",
              title: "分类类型",
              searchList: $.getJSON("ajax/category"),
              formatter: Table.api.formatter.label,
            },
            { field: "sort", title: "排序", operate: false },
            {
              field: "status",
              title: __("Status"),
              searchList: { 0: "禁用", 1: "启用" },
              operate: false,
              formatter: Table.api.formatter.status,
            },
            {
              field: "updatetime",
              title: "操作时间",
              operate: false,
              addclass: "datetimerange",
              autocomplete: false,
              formatter: Table.api.formatter.datetime,
            },
            {
              field: "operate",
              title: __("Operate"),
              table: table,
              events: Table.api.events.operate,
              formatter: Table.api.formatter.operate,
            },
          ],
        ],
      };
      // 初始化表格
      table.bootstrapTable(tableOptions);

      // 为表格绑定事件
      Table.api.bindevent(table);

      //绑定TAB事件
      // $('a[data-toggle="tab"]').on("shown.bs.tab", function (e) {
      //   // var options = table.bootstrapTable(tableOptions);
      //   var typeStr = $(this).attr("href").replace("#", "");
      //   var options = table.bootstrapTable("getOptions");
      //   options.pageNumber = 1;
      //   options.queryParams = function (params) {
      //     // params.filter = JSON.stringify({type: typeStr});
      //     params.type = typeStr;

      //     return params;
      //   };
      //   table.bootstrapTable("refresh", {});
      //   return false;
      // });

      //必须默认触发shown.bs.tab事件
      // $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
    },
    add: function () {
      Controller.api.bindevent();
      setTimeout(function () {
        $("#c-type").trigger("change");
      }, 100);
    },
    edit: function () {
      Controller.api.bindevent();
    },
    api: {
      bindevent: function () {
        $(document).on("change", "#c-type", function () {
          $("#c-pid option[data-type='all']").prop("selected", true);
          $("#c-pid option").removeClass("hide");
          $(
            "#c-pid option[data-type!='" +
              $(this).val() +
              "'][data-type!='all']"
          ).addClass("hide");
          $("#c-pid").data("selectpicker") &&
            $("#c-pid").selectpicker("refresh");
        });
        Form.api.bindevent($("form[role=form]"));
      },
    },
  };
  return Controller;
});
